<?php namespace Phpcmf\Controllers\Member;

class Home extends \Phpcmf\Table {

    public function __construct(...$params)
    {
        parent::__construct(...$params);
        // 表单显示名称
        $this->name = dr_lang('订单');
        $this->is_list_data = 0;
    }


    public function index() {

        // 分类
        $type = [
            0 => [
                'name' => dr_lang('所有订单'),
                'icon' => '<i class="fa fa-shopping-cart"></i>',
                'url' => \Phpcmf\Service::L('Router')->member_url('order/home/index', ['tid' => 0]),
            ],
            1 => [
                'name' => dr_lang('未付款'),
                'icon' => '<i class="fa fa-rmb"></i>',
                'url' => \Phpcmf\Service::L('Router')->member_url('order/home/index', ['tid' => 1]),
            ],
            2 => [
                'name' => dr_lang('已付款'),
                'icon' => '<i class="fa fa-check-square-o"></i>',
                'url' => \Phpcmf\Service::L('Router')->member_url('order/home/index', ['tid' => 2]),
            ],
            3 => [
                'name' => dr_lang('已发货'),
                'icon' => '<i class="fa fa-truck"></i>',
                'url' => \Phpcmf\Service::L('Router')->member_url('order/home/index', ['tid' => 3]),
            ],
            4 => [
                'name' => dr_lang('已完成'),
                'icon' => '<i class="fa fa-check-circle"></i>',
                'url' => \Phpcmf\Service::L('Router')->member_url('order/home/index', ['tid' => 4]),
            ],
        ];

        $tid = (int)$_GET['tid'];
        switch ($tid) {

            case 1:
                $where = '`uid`='.$this->uid.' AND `status` = 1';
                break;

            case 2:
                $where = '`uid`='.$this->uid.' AND `status` = 2';
                break;

            case 3:
                $where = '`uid`='.$this->uid.' AND `status` = 3';
                break;

            case 4:
                $where = '`uid`='.$this->uid.' AND `status` = 9';
                break;

            default:
                $where = '`uid`='.$this->uid;
                break;
        }

        $this->my_field = array(
            'sn' => array(
                'ismain' => 1,
                'name' => dr_lang('订单编号'),
                'fieldname' => 'sn',
                'fieldtype' => 'Text',
            ),
            'name' => array(
                'ismain' => 1,
                'name' => dr_lang('收货人'),
                'fieldname' => 'name',
                'fieldtype' => 'Text',
            ),
            'phone' => array(
                'ismain' => 1,
                'name' => dr_lang('电话'),
                'fieldname' => 'phone',
                'fieldtype' => 'Text',
            ),
            'address' => array(
                'ismain' => 1,
                'name' => dr_lang('地址'),
                'fieldname' => 'address',
                'fieldtype' => 'Text',
            ),
            'remark' => array(
                'ismain' => 1,
                'name' => dr_lang('备注'),
                'fieldname' => 'remark',
                'fieldtype' => 'Text',
            ),
        );
        // 初始化数据表
        $this->_init([
            'table' => SITE_ID.'_order',
            'field' => $this->my_field,
            'order_by' => 'inputtime desc',
            'date_field' => 'inputtime',
            'where_list' => $where,
        ]);
        list($a, $data) = $this->_List(['tid' => $tid]);

        $list = [];
        foreach ($data['list'] as $t) {
            $t['goods'] = \Phpcmf\Service::M('order', 'order')->get_item($t['id']);
            $list[] = $t;
        }

        \Phpcmf\Service::V()->assign([
            'type' => $type,
            'list' => $list,
            'field' => $this->my_field,
        ]);
        \Phpcmf\Service::V()->display('buy_list.html');
    }


    // 订单详情
    public function show() {

        // 初始化数据表
        $this->_init(['table' => SITE_ID.'_order']);

        $id = (int)\Phpcmf\Service::L('input')->get('id');
        list($tpl, $data) = $this->_Show($id);
        if (!$data) {
            $this->_msg(0, dr_lang('订单不存在'));
        } elseif ($data['uid'] != $this->uid) {
            $this->_msg(0, dr_lang('无权限查看此订单'));
        }

        $field = \Phpcmf\Service::M('order', 'order')->get_field(\Phpcmf\Service::M('app')->get_config(APP_DIR), $data['mid']);
        if ($field) {
            \Phpcmf\Service::L('Field')->app(APP_DIR);
            $myfield = \Phpcmf\Service::L('Field')->toform($id, $field, $data, 1);
        } else {
            $myfield = '';
        }

        \Phpcmf\Service::V()->assign([
            'order' => $data,
            'goods' => \Phpcmf\Service::M('order', 'order')->get_item($id),
            'myfield' => $myfield,
        ]);
        \Phpcmf\Service::V()->display('buy_show.html');
    }


    // 收货
    public function shouhuo() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $order = \Phpcmf\Service::M('order', 'order')->info($id);
        !$order && exit($this->_json(0, dr_lang('订单不存在')));
        $this->uid != $order['uid'] && exit($this->_json(0, dr_lang('无权限操作此订单')));
        $order['status'] != 3 && exit($this->_json(0, dr_lang('此订单还未发货')));

        $rt = \Phpcmf\Service::M('order', 'order')->shouhuo($order, dr_lang('买家确认收货'));
        $rt && !$rt['code'] && $this->_json(0, $rt['msg']);
        $this->_json(1, dr_lang('操作成功'));
    }

}
